package com.scaler7.admin.controller;


import com.scaler7.admin.domain.Article;
import com.scaler7.admin.domain.Categories;
import com.scaler7.admin.service.IArticleService;
import com.scaler7.admin.vo.CategoriesVO;
import com.scaler7.utils.CommonUtil;
import com.scaler7.utils.WebUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.scaler7.admin.domain.Comments;
import com.scaler7.admin.service.ICommentsService;
import com.scaler7.common.Constant;
import com.scaler7.common.Result;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author scaler7
 * @since 2019-12-01
 */
@Controller
@RequestMapping("/admin/comments")
public class CommentsController {

	@Autowired
	ICommentsService commentsService;
	@Autowired
	IArticleService articleService;

	@GetMapping(value = "")
	public ModelAndView loadAllComments(
			ModelAndView modelAndView,
			@RequestParam(name = "pageNum",required = true,defaultValue = "1")
			Integer pageNum,
			@RequestParam(name = "pageSize",required = true,defaultValue = "10")
			Integer pageSize
			) {
		PageInfo<Comments> comments = commentsService.getCommentsOnePage(pageNum, pageSize);
		modelAndView.addObject("comments", comments);
		modelAndView.setViewName("admin/comment_list");
		return modelAndView;
	}
	//删除评论
	@RequiresPermissions("comment:delete")
	@PostMapping("delete")
	@ResponseBody
	public Object removeComments(Integer id) {
		commentsService.removeComments(id);
		return new Result();
	}

	//评论通过
	@RequiresPermissions("comment:pass")
	@PostMapping("pass")
	@ResponseBody
	public Object passComments(Integer id) {
		Comments comments = new Comments();
		comments.setId(id);
		comments.setStatus(Constant.STATUS_PASS);
		commentsService.passComment(comments);
		return new Result();
	}
	//新增评论
	@RequiresPermissions("comment:save")
	@PostMapping("save")
	@ResponseBody
	public Object saveComments(
			@RequestParam(name = "id", required = true)
					Integer id,
			@RequestParam(name = "articleId", required = true)
					String title,
			@RequestParam(name = "authorId", required = false)
					String titlePic,
			@RequestParam(name = "slug", required = false)
					String slug,
			@RequestParam(name = "content", required = true)
					String content,
			@RequestParam(name = "type", required = true)
					String type,
			@RequestParam(name = "status", required = true)
					String status,
			@RequestParam(name = "tags", required = false)
					String tags,
			@RequestParam(name = "categories", required = false, defaultValue = "默认分类")
					String categories,
			@RequestParam(name = "allowComment", required = true)
					Boolean allowComment
	) {
		Article article = articleService.getOneArticle(id);//获取作者ID

		Comments comments = new Comments();
		//要传数据的部分
		comments.setContent(content);
		//不用传数据的部分
//		WebUtil.getRequest().getRemoteAddr(), // 登陆ip
		comments.setUrl("不用了");
		comments.setCreated(CommonUtil.getTimeFormat(Constant.YYYY_MM_DD_HH_MM_SS));
		comments.setStatus(Constant.STATUS_NOT_PASS);
		comments.setType("comment");
		commentsService.saveComment(comments);
		return new Result();
	}
}
